package com.example.mapp.buildSql;

import com.example.entity.PointFlow;
import com.example.util.StringUtil;

/**
 * 查询积分
 */
public class PointFlowMappSql {
    /**
     *
     * 查询的SQL
     * findList
     * @param
     * @return
     */
    public String findList(PointFlow pointFlow){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  id AS id, ");
        sb.append("  operation_type AS 'operationType', ");
        sb.append("  operation_content AS 'operationContent', ");
        sb.append("  identification AS 'identification',");
        sb.append("  mark AS 'mark',");
        sb.append("  create_by AS 'createBy', ");
        sb.append("  create_date AS 'createDate', ");
        sb.append("  update_by AS 'updateBy', ");
        sb.append("  user_name AS 'userName', ");
        sb.append("  party_name AS 'partyName', ");
        sb.append("  function AS 'function', ");
        sb.append("  extend_field AS 'extendField', ");
        sb.append("  update_date AS 'updateDate'");
        sb.append("  from point_flow  where 1=1 ");


        if(pointFlow.getId() !=null){
            sb.append(" and id="+pointFlow.getId());
        }
        if(StringUtil.isNotBlank(pointFlow.getOperationType())){
            sb.append(" and operation_type='"+pointFlow.getOperationType()+"'");
        }
        if(StringUtil.isNotBlank(pointFlow.getIdentification())){
            sb.append(" and identification='"+pointFlow.getIdentification()+"'");
        }
        if(StringUtil.isNotBlank(pointFlow.getExtendField())){
            sb.append(" and extend_field='"+pointFlow.getExtendField()+"'");
        }
        sb.append(" order by create_date desc ");
        return sb.toString();
    }

    /**
     *
     * 查询的最新的SQL
     * findList
     * @param
     * @return
     */
    public String findOne(PointFlow pointFlow){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  id AS id, ");
        sb.append("  operation_type AS 'operationType', ");
        sb.append("  operation_content AS 'operationContent', ");
        sb.append("  identification AS 'identification',");
        sb.append("  mark AS 'mark',");
        sb.append("  create_by AS 'createBy', ");
        sb.append("  create_date AS 'createDate', ");
        sb.append("  update_by AS 'updateBy', ");
        sb.append("  user_name AS 'userName', ");
        sb.append("  party_name AS 'partyName', ");
        sb.append("  function AS 'function', ");
        sb.append("  extend_field AS 'extendField', ");
        sb.append("  update_date AS 'updateDate'");
        sb.append("  from point_flow  where 1=1 ");

        sb.append(" and identification='"+pointFlow.getIdentification()+"'");
        sb.append(" order by create_date desc limit 1 ");
        return sb.toString();
    }

    /**
     * 查询用户角色信息
     */
    public String findUserInfo(Long id){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  pa.party_name AS 'partyName', ");
        sb.append("  p_us.user_id AS 'userId', ");
        sb.append("  (case when s_us.mark is not null then s_us.mark else 0 end)  as 'mark', ");
        sb.append("  s_us.user_name AS 'userName' ");
        sb.append("  from party pa ");
        sb.append("  left join party_user p_us ON pa.id = p_us.party_id ");
        sb.append("  left join sys_user s_us ON p_us.user_id = s_us.id ");
        sb.append("  where pa.id ="+id);
        sb.append("  and s_us.type='party' and s_us.user_state='1' ");

        return sb.toString();
    }

    /**
     * 根据userId查询用户角色信息
     */
    public String findUserInfoByUserId(Long id){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  sys.id as 'userId', ");
        sb.append("  sys.user_name as 'userName', ");
        sb.append("  ty.party_name as 'partyName', ");
        sb.append("  (case when sys.mark is not null then sys.mark else 0 end ) as 'mark' ");
        sb.append("  from sys_user sys ");
        sb.append("  left join party_user par ON sys.id = par.user_id ");
        sb.append("  left join party ty ON ty.id=par.party_id ");
        sb.append("  where sys.id="+id);
        return sb.toString();
    }

    /**
     * 根据userId查询系统管理员用户角色信息
     */
    public String getAdminInfoByUserId(Long id){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  sys.id as 'userId', ");
        sb.append("  sys.user_name as 'userName', ");
        sb.append("  ty.party_name as 'partyName', ");
        sb.append("  (case when sys.mark is not null then sys.mark else 0 end ) as 'mark' ");
        sb.append("  from sys_user sys ");
        sb.append("  left join party_user par ON sys.id = par.user_id ");
        sb.append("  left join party ty ON ty.id=par.party_id ");
        sb.append("  where par.party_id=(select party_id  from party_user pu where pu.user_id=" + id + ")");
        sb.append("  and sys.type='party' and sys.user_state='1' ");

        return sb.toString();
    }

}
